package com.example.permission.service.impl;

import com.example.common.enetiy.User;
import com.example.common.mapper.UserMapper;
import com.example.common.model.param.UserParam;
import com.example.common.utils.Assert;
import com.example.permission.entity.SystemRole;
import com.example.permission.mapper.SystemRoleMapper;
import com.example.permission.mapper.SystemUserRoleMapper;
import com.example.permission.model.req.UpdateSystemDTO;
import com.example.permission.model.resp.SystemUserVO;
import com.example.permission.service.SystemUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qwer3
 * @version 1.0.0
 * @create 2025/11/15 21:37
 */
@Service
@RequiredArgsConstructor
public class SystemUserServiceImpl implements SystemUserService {
    private final UserMapper userMapper;
    private final SystemUserRoleMapper userRoleMapper;
    private final SystemRoleMapper systemRoleMapper;

    @Override
    public List<SystemUserVO> getSystemUser(String nickname) {
        UserParam userParam = UserParam.builder().nickname(nickname).build();
        List<SystemUserVO> users = userRoleMapper.querySystemUserByParam(userParam);
        for (SystemUserVO user : users) {
            user.setStatus(user.isDelete() ? "禁用" : "正常");
        }
        return users;
    }

    @Override
    public void disableSystemUser(String userId) {
        User user = userMapper.queryUserByUserId(userId);
        if (user == null) {
            throw new RuntimeException("用户删除失败");
        }
        userMapper.disableSystemUser(userId);
    }

    @Override
    public void updateSystemUser(UpdateSystemDTO updateSystemDTO) {
        SystemRole systemRole = systemRoleMapper.querySystemRoleByRoleId(updateSystemDTO.getRoleId());
        Assert.notNull(systemRole, "角色不存在");
        userRoleMapper.saveUserRole(updateSystemDTO.getUserId(), systemRole.getRoleId());
    }
}
